package com.jumi.microservice.utils;

import com.alibaba.fastjson.JSONArray;
import com.jumi.microservice.desdzfp.DESDZFP;
import com.jumi.microservice.service.invoice.invoiceConfig.InvoiceConfig;
import com.jumi.microservice.utils.nuonuo.*;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.SimpleHttpConnectionManager;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

@Component
@RefreshScope
public class NuoNuoUtil {

    private static final Logger log = LoggerFactory.getLogger(NuoNuoUtil.class);
//    @Value("${invoice.sendUrl}")
//    private String sendUrl;
//    @Value("${invoice.queryUrl}")
//    private String queryUrl;
//    @Value("${invoice.voidInvoice}")
//    private String voidInvoice;
//    @Value("${invoice.redInvoice}")
//    private String redInvoice;
    @Autowired
    InvoiceConfig invoiceConfig;

    public String Invoice(ThirdCommonParametersRequest<ThirdPartyInvoiceRequest> request) {
        log.info("第三方开票参数" + JSONArray.toJSONString(request));
        String order = JSONArray.toJSONString(request);
        order = DESDZFP.encrypt(order);
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        try {
            httpClient = new HttpClient();
            postMethod = new PostMethod(invoiceConfig.getSendUrl());
            postMethod.setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            postMethod.addParameter("order", order);
            httpClient.executeMethod(postMethod);
            String info = new String(postMethod.getResponseBody(), StandardCharsets.UTF_8);
            log.info("开票返回结果" + JSONArray.toJSONString(info));
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            assert postMethod != null;
            postMethod.releaseConnection();
            ((SimpleHttpConnectionManager) httpClient.getHttpConnectionManager()).shutdown();
        }
        return "";
    }

    public String QueryInvoice(QueryThirdCommonRequest request) {
        log.info("查询第三方开票流水号/订单号" + JSONArray.toJSONString(request));
        String order = JSONArray.toJSONString(request);
        order = DESDZFP.encrypt(order);
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        try {
            httpClient = new HttpClient();
            postMethod = new PostMethod(invoiceConfig.getQueryUrl());
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            //添加参数
            postMethod.addParameter("order", order);
            //执行
            httpClient.executeMethod(postMethod);
            //接口返回信息
            String info = new String(postMethod.getResponseBody(), StandardCharsets.UTF_8);
            log.info("第三方发票查询返回结果是" + JSONArray.toJSONString(info));
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String voidInvoice(ThirdCommonParametersRequest<VoidInvoiceRequest> request) {
        String order = JSONArray.toJSONString(request);
        log.info("发票作废数据" + order);
        order = DESDZFP.encrypt(order);
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        try {
            httpClient = new HttpClient();
            postMethod = new PostMethod(invoiceConfig.getVoidInvoice());
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            //添加参数
            postMethod.addParameter("order", order);
            //执行
            httpClient.executeMethod(postMethod);
            //接口返回信息
            String info = new String(postMethod.getResponseBody(), StandardCharsets.UTF_8);
            log.info("返回数据" + info);
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }

    public String redInvoice(ThirdCommonParametersRequest<RedInvoiceRequest> request) {
        String order = JSONArray.toJSONString(request);
        log.info("红票申请" + order);
        order = DESDZFP.encrypt(order);
        HttpClient httpClient = null;
        PostMethod postMethod = null;
        try {
            httpClient = new HttpClient();
            postMethod = new PostMethod(invoiceConfig.getRedInvoice());
            postMethod.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, "UTF-8");
            //添加参数
            postMethod.addParameter("order", order);
            //执行
            httpClient.executeMethod(postMethod);
            //接口返回信息
            String info = new String(postMethod.getResponseBody(), "UTF-8");
            log.info("红票申请返回数据" + info);
            return info;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "";
    }
}
